package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //开启事务
    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
/**
 * 保存菜品数据
 * 1.创建dish对象
 * 2.将dishdto拷贝到dish对象中
 * 3.将dish插入到表中（注意：需要插入以后的返回值id）
 * 4。从dishDTO中获取flavors集合
 * 5.遍历flavors集合，将每个flavor对象中的dishId设置为刚刚插入的dish的id
 * 6.将flavor插入到表中
 */
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insert(dish);

        dishDTO.getFlavors().forEach(dishFlavor -> {

                dishFlavor.setDishId(dish.getId());
        });

        dishFlavorMapper.insertBatch(dishDTO.getFlavors());
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //使用pagehelper插件自动简化查询
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page page=dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void delete(Long[] ids) {
        /**
        * 删除菜品数据
        * 1.遍历数组获取id
        * 2.如果当前菜品启售中，不能删
         * 4.如果当前菜品被套餐关联，不能删
         * 5.如果当前菜品有口味，删除
        */
        for (Long id : ids){
            Dish dish=dishMapper.findById(id);
            if (dish.getStatus()==StatusConstant.ENABLE){
                //当前菜品启售中，不能删
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            List<SetmealDish> setmealDishes=setmealDishMapper.getByDishId(id);
            if (setmealDishes!=null && setmealDishes.size()>0){
                //当前菜品被套餐关联，不能删
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //菜品可删除
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        /**
         * 1.在Dish表中查询Dish
         * 2.在DishFlabor表中查询DishFlavors
         * 3.Dish+dishFlavors封装到DishVO中返回
         */
        Dish dish=dishMapper.findById(id);
        List<DishFlavor> dishFlavors=dishFlavorMapper.findByDishId(id);
        DishVO dishVO=new DishVO();
        //拷贝属性
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        /**
         * 1.修改Dish表(DIshDTO->Dish)
         * 2.删除Dish_flavor表
         * 3.批量插入Dish_flavor表
         */
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors=dishDTO.getFlavors();
        dishFlavorMapper.insertBatch(flavors);

    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish= Dish.builder().
                status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish=Dish.builder().
                categoryId(categoryId).
                status(StatusConstant.ENABLE).
                build();
        return dishMapper.list(dish);
    }
}
